/*
 * cryptomfs - a macfuse implementation of cryptofs.
 * Copyright (C) 2008  Greg J. Hedlund
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "FSOps.h"


@implementation FSOps

+(BOOL)createFilesystemAtPath:(NSString*)path
				   volumename:(NSString*)volumename
				   volumeicon:(NSString*)volumeicon
					 copyicon:(BOOL)copyIcon
{
	BOOL retVal = YES;
	// run the mkcryptomfs command with selected options
	NSMutableArray *args = [NSMutableArray arrayWithCapacity:3];
	
	NSString * defaultIconPath = @"@CRYPT_PKG/volicon.icns";
	NSString * iconPath = volumeicon;
	if(copyIcon) {
		iconPath = defaultIconPath;
	}
	
	[args addObject:[NSString stringWithFormat:@"--volumename=%@", volumename]];
	[args addObject:[NSString stringWithFormat:@"--volumeicon=%@", iconPath]];
	[args addObject:path];
	
	NSString * exePath = [[[NSBundle mainBundle] executablePath] stringByDeletingLastPathComponent];
	NSString * mkcryptomfs = [exePath stringByAppendingPathComponent:@"mkcryptomfs"];
	
	NSTask * mkTask = [[[NSTask alloc] init] autorelease];
	[mkTask setLaunchPath:mkcryptomfs];
	[mkTask setArguments:args];
	
	[mkTask launch];
	[mkTask waitUntilExit];
	
	if([mkTask terminationStatus] != 0) {
		NSAlert* alert = 
		[NSAlert alertWithMessageText:@"Cryptomfs"
						defaultButton:nil
					  alternateButton:nil
						  otherButton:nil
			informativeTextWithFormat:@"Could not create filesystem: '%@'", path];
		[alert runModal];
		retVal = NO;
	} else {
		if(copyIcon) {
			// copy icon to default icon path
			NSError *err = nil;
			NSString * destPath = [NSString pathWithComponents:[NSArray arrayWithObjects:path, @"volicon.icns", NULL]];
			NSFileManager * fileManager = [NSFileManager defaultManager];
			[fileManager copyItemAtPath:volumeicon
								 toPath:(NSString *)destPath error:&err];
			if(err != nil) {
				NSAlert* alert = 
				[NSAlert alertWithMessageText:@"Cryptomfs"
								defaultButton:nil
							  alternateButton:nil
								  otherButton:nil
					informativeTextWithFormat:@"Could not copy icon %@ to %@", volumeicon, destPath];
                [alert runModal];
				retVal = NO;
			}
		}
	}
	return retVal;
}

+(void)mountFilesystemAtPath:(NSString*)path
{
	// run the mkcryptomfs command with selected options
	NSMutableArray *args = [NSMutableArray arrayWithCapacity:2];
	
	[args addObject:@"--dialog"];
	[args addObject:path];
	
	NSString * exePath = [[[NSBundle mainBundle] executablePath] stringByDeletingLastPathComponent];
	NSString * mkcryptomfs = [exePath stringByAppendingPathComponent:@"mount_cryptomfs"];
	
	NSTask * mkTask = [[[NSTask alloc] init] autorelease];
	[mkTask setLaunchPath:mkcryptomfs];
	[mkTask setArguments:args];
	
	[mkTask launch];
	[mkTask waitUntilExit];
	
	if([mkTask terminationStatus] != 0) {
		NSAlert* alert = 
		[NSAlert alertWithMessageText:@"Cryptomfs"
						defaultButton:nil
					  alternateButton:nil
						  otherButton:nil
			informativeTextWithFormat:@"Could not mount filesystem: '%@'", path];
		[alert runModal];
	}
}

@end
